{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "import vectorbt as vbt\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/mnt/recoverData/linuxProgram/workspace/vectorbt_2/__pypackages__/3.9/lib/dateparser/date_parser.py:35: PytzUsageWarning: The localize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html\n",
      "  date_obj = stz.localize(date_obj)\n",
      "/mnt/recoverData/linuxProgram/workspace/vectorbt_2/__pypackages__/3.9/lib/vectorbt/data/base.py:527: UserWarning: Symbols have mismatching index. Setting missing data points to NaN.\n",
      "  data = cls.align_index(data, missing=missing_index)\n"
     ]
    }
   ],
   "source": [
    "start_date = '2009-01-01'\n",
    "end_date = '2021-10-31'\n",
    "DOW_30_TICKER = ['AXP', 'AMGN', 'AAPL', 'BA', 'CAT', 'CSCO', 'CVX', 'GS', 'HD', 'HON', 'IBM', 'INTC', 'JNJ', 'KO', 'JPM', 'MCD', 'MMM', 'MRK', 'MSFT', 'NKE', 'PG', 'TRV', 'UNH', 'CRM', 'VZ', 'V', 'WBA', 'WMT', 'DIS', 'DOW']\n",
    "alpaca_data = vbt.YFData.download(\n",
    "     DOW_30_TICKER,\n",
    "     start=start_date,\n",
    "     end=end_date,\n",
    "     interval='1D'\n",
    " )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [],
   "source": [
    "open = alpaca_data.get()[0].stack(dropna=False).rename('open')\n",
    "high = alpaca_data.get()[1].stack(dropna=False).rename('high')\n",
    "low = alpaca_data.get()[2].stack(dropna=False).rename('low')\n",
    "close = alpaca_data.get()[3]\n",
    "price_close = close.stack(dropna=False).rename('close')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.43 s ± 17.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>symbol</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>rsi_30</th>\n",
       "      <th>ma_30</th>\n",
       "      <th>ma_60</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2008-12-31 00:00:00+00:00</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>2.625211</td>\n",
       "      <td>2.679260</td>\n",
       "      <td>2.605973</td>\n",
       "      <td>2.606278</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2008-12-31 00:00:00+00:00</td>\n",
       "      <td>AMGN</td>\n",
       "      <td>43.437676</td>\n",
       "      <td>44.281939</td>\n",
       "      <td>43.399647</td>\n",
       "      <td>43.924458</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2008-12-31 00:00:00+00:00</td>\n",
       "      <td>AXP</td>\n",
       "      <td>14.442320</td>\n",
       "      <td>15.069199</td>\n",
       "      <td>14.394099</td>\n",
       "      <td>14.908461</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2008-12-31 00:00:00+00:00</td>\n",
       "      <td>BA</td>\n",
       "      <td>31.195813</td>\n",
       "      <td>32.290929</td>\n",
       "      <td>31.128306</td>\n",
       "      <td>32.005898</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2008-12-31 00:00:00+00:00</td>\n",
       "      <td>CAT</td>\n",
       "      <td>29.963724</td>\n",
       "      <td>30.923658</td>\n",
       "      <td>29.963724</td>\n",
       "      <td>30.628820</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96925</th>\n",
       "      <td>2021-10-29 00:00:00+00:00</td>\n",
       "      <td>UNH</td>\n",
       "      <td>449.991602</td>\n",
       "      <td>456.903744</td>\n",
       "      <td>448.654722</td>\n",
       "      <td>455.992676</td>\n",
       "      <td>65.073663</td>\n",
       "      <td>413.718763</td>\n",
       "      <td>412.824563</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96926</th>\n",
       "      <td>2021-10-29 00:00:00+00:00</td>\n",
       "      <td>V</td>\n",
       "      <td>208.105676</td>\n",
       "      <td>212.542125</td>\n",
       "      <td>207.439200</td>\n",
       "      <td>210.652161</td>\n",
       "      <td>44.866255</td>\n",
       "      <td>224.879986</td>\n",
       "      <td>227.021133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96927</th>\n",
       "      <td>2021-10-29 00:00:00+00:00</td>\n",
       "      <td>VZ</td>\n",
       "      <td>51.250939</td>\n",
       "      <td>51.787852</td>\n",
       "      <td>51.163080</td>\n",
       "      <td>51.729282</td>\n",
       "      <td>46.768944</td>\n",
       "      <td>51.809784</td>\n",
       "      <td>52.472365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96928</th>\n",
       "      <td>2021-10-29 00:00:00+00:00</td>\n",
       "      <td>WBA</td>\n",
       "      <td>45.414664</td>\n",
       "      <td>45.821708</td>\n",
       "      <td>45.327440</td>\n",
       "      <td>45.569729</td>\n",
       "      <td>42.857140</td>\n",
       "      <td>46.521764</td>\n",
       "      <td>47.068581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96929</th>\n",
       "      <td>2021-10-29 00:00:00+00:00</td>\n",
       "      <td>WMT</td>\n",
       "      <td>146.217791</td>\n",
       "      <td>148.382738</td>\n",
       "      <td>145.871790</td>\n",
       "      <td>147.710510</td>\n",
       "      <td>57.362685</td>\n",
       "      <td>140.781033</td>\n",
       "      <td>143.417333</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>96930 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                           Date symbol        open        high         low  \\\n",
       "0     2008-12-31 00:00:00+00:00   AAPL    2.625211    2.679260    2.605973   \n",
       "1     2008-12-31 00:00:00+00:00   AMGN   43.437676   44.281939   43.399647   \n",
       "2     2008-12-31 00:00:00+00:00    AXP   14.442320   15.069199   14.394099   \n",
       "3     2008-12-31 00:00:00+00:00     BA   31.195813   32.290929   31.128306   \n",
       "4     2008-12-31 00:00:00+00:00    CAT   29.963724   30.923658   29.963724   \n",
       "...                         ...    ...         ...         ...         ...   \n",
       "96925 2021-10-29 00:00:00+00:00    UNH  449.991602  456.903744  448.654722   \n",
       "96926 2021-10-29 00:00:00+00:00      V  208.105676  212.542125  207.439200   \n",
       "96927 2021-10-29 00:00:00+00:00     VZ   51.250939   51.787852   51.163080   \n",
       "96928 2021-10-29 00:00:00+00:00    WBA   45.414664   45.821708   45.327440   \n",
       "96929 2021-10-29 00:00:00+00:00    WMT  146.217791  148.382738  145.871790   \n",
       "\n",
       "            close     rsi_30       ma_30       ma_60  \n",
       "0        2.606278        NaN         NaN         NaN  \n",
       "1       43.924458        NaN         NaN         NaN  \n",
       "2       14.908461        NaN         NaN         NaN  \n",
       "3       32.005898        NaN         NaN         NaN  \n",
       "4       30.628820        NaN         NaN         NaN  \n",
       "...           ...        ...         ...         ...  \n",
       "96925  455.992676  65.073663  413.718763  412.824563  \n",
       "96926  210.652161  44.866255  224.879986  227.021133  \n",
       "96927   51.729282  46.768944   51.809784   52.472365  \n",
       "96928   45.569729  42.857140   46.521764   47.068581  \n",
       "96929  147.710510  57.362685  140.781033  143.417333  \n",
       "\n",
       "[96930 rows x 9 columns]"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def preprocess_vectorbt(close):\n",
    "    rsi_30 = vbt.RSI.run(close, 30).rsi.stack(dropna=False).rename(columns = {30 : 'rsi_30'})\n",
    "    ma_30 = vbt.MA.run(close, 30).ma.stack(dropna=False).rename(columns = {30 : 'ma_30'})\n",
    "    ma_60 = vbt.MA.run(close, 60).ma.stack(dropna=False).rename(columns = {60 : 'ma_60'})\n",
    "    res = pd.concat([open,high,low,price_close,rsi_30,ma_30,ma_60],axis=1).reset_index() # is slow because this line\n",
    "    return res\n",
    "\n",
    "%timeit preprocess_vectorbt(close)\n",
    "res = preprocess_vectorbt(close)\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "68.6 ms ± 574 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>symbol</th>\n",
       "      <th>rsi_30</th>\n",
       "      <th>ma_30</th>\n",
       "      <th>ma_60</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2008-12-31 00:00:00+00:00</td>\n",
       "      <td>AAPL</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2008-12-31 00:00:00+00:00</td>\n",
       "      <td>AMGN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2008-12-31 00:00:00+00:00</td>\n",
       "      <td>AXP</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2008-12-31 00:00:00+00:00</td>\n",
       "      <td>BA</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2008-12-31 00:00:00+00:00</td>\n",
       "      <td>CAT</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96925</th>\n",
       "      <td>2021-10-29 00:00:00+00:00</td>\n",
       "      <td>UNH</td>\n",
       "      <td>65.073663</td>\n",
       "      <td>413.718763</td>\n",
       "      <td>412.824563</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96926</th>\n",
       "      <td>2021-10-29 00:00:00+00:00</td>\n",
       "      <td>V</td>\n",
       "      <td>44.866255</td>\n",
       "      <td>224.879986</td>\n",
       "      <td>227.021133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96927</th>\n",
       "      <td>2021-10-29 00:00:00+00:00</td>\n",
       "      <td>VZ</td>\n",
       "      <td>46.768944</td>\n",
       "      <td>51.809784</td>\n",
       "      <td>52.472365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96928</th>\n",
       "      <td>2021-10-29 00:00:00+00:00</td>\n",
       "      <td>WBA</td>\n",
       "      <td>42.857140</td>\n",
       "      <td>46.521764</td>\n",
       "      <td>47.068581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96929</th>\n",
       "      <td>2021-10-29 00:00:00+00:00</td>\n",
       "      <td>WMT</td>\n",
       "      <td>57.362685</td>\n",
       "      <td>140.781033</td>\n",
       "      <td>143.417333</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>96930 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                           Date symbol     rsi_30       ma_30       ma_60\n",
       "0     2008-12-31 00:00:00+00:00   AAPL        NaN         NaN         NaN\n",
       "1     2008-12-31 00:00:00+00:00   AMGN        NaN         NaN         NaN\n",
       "2     2008-12-31 00:00:00+00:00    AXP        NaN         NaN         NaN\n",
       "3     2008-12-31 00:00:00+00:00     BA        NaN         NaN         NaN\n",
       "4     2008-12-31 00:00:00+00:00    CAT        NaN         NaN         NaN\n",
       "...                         ...    ...        ...         ...         ...\n",
       "96925 2021-10-29 00:00:00+00:00    UNH  65.073663  413.718763  412.824563\n",
       "96926 2021-10-29 00:00:00+00:00      V  44.866255  224.879986  227.021133\n",
       "96927 2021-10-29 00:00:00+00:00     VZ  46.768944   51.809784   52.472365\n",
       "96928 2021-10-29 00:00:00+00:00    WBA  42.857140   46.521764   47.068581\n",
       "96929 2021-10-29 00:00:00+00:00    WMT  57.362685  140.781033  143.417333\n",
       "\n",
       "[96930 rows x 5 columns]"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "def preprocess_vectorbt_raw(close):\n",
    "    rsi_30 = vbt.RSI.run(close, 30).rsi.stack(dropna=False).rename(columns = {30 : 'rsi_30'})\n",
    "    ma_30 = vbt.MA.run(close, 30).ma.stack(dropna=False).rename(columns = {30 : 'ma_30'})\n",
    "    ma_60 = vbt.MA.run(close, 60).ma.stack(dropna=False).rename(columns = {60 : 'ma_60'})\n",
    "    res = pd.concat([rsi_30,ma_30,ma_60],axis=1).reset_index() # optimization possible in this line\n",
    "    return res\n",
    "\n",
    "%timeit preprocess_vectorbt_raw(close)\n",
    "res = preprocess_vectorbt_raw(close)\n",
    "res"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "/gnu/store/fby6l226w8kh2mwkzpjpajmgy0q1kxli-python-wrapper-3.9.9/bin/python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
